
package com.koala.upms.controller;

import com.koala.common.core.constant.CommonConstants;
import com.koala.common.core.dto.MenuTree;
import com.koala.common.core.utils.RT;
import com.koala.common.core.utils.TreeUtil;
import com.koala.common.security.util.SecurityUtils;
import com.koala.upms.service.SysMenuService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;


/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author zhangchengji
 * @since 2019-05-12
 */

@RestController
@AllArgsConstructor
@RequestMapping("/menu" )
public class SysMenuController  {

private final SysMenuService sysMenuService;

@GetMapping
public RT getUserMenu(){
    List<MenuTree> menuTrees=new ArrayList<>();
    SecurityUtils.getRoles().forEach(roleId ->{ menuTrees.addAll(sysMenuService.getMenuByRoleId(roleId).stream().filter(menu -> CommonConstants.MENU.equals(menu.getType())).map(MenuTree::new).sorted(Comparator.comparingInt(MenuTree::getSort)).collect(Collectors.toList()));});
    return new RT<>(TreeUtil.buildByLoop(menuTrees,-1));
}



/**
* 分页查询信息
*
* @param params 分页对象
* @return 分页对象
*//*

@RequestMapping("/page" )
public Page page(@RequestParam Map<String, Object> params){
        params.put(CommonConstant.DEL_FLAG,CommonConstant.STATUS_NORMAL);
        return sysMenuService.selectPage(new Query<>(params),new EntityWrappeRT<>());
        }

*/
/**
 * 添加
 * @param  sysMenu 实体
 * @return success/false
 *//*

@PostMapping
public RT<Boolean> add(@RequestBody SysMenu sysMenu){
        return new RT<>(sysMenuService.insert(sysMenu));
        }

*/
/**
 * 删除
 * @param id ID
 * @return success/false
 *//*

@DeleteMapping("/{id}" )
public RT<Boolean> delete(@PathVariable Integer id){
    SysMenu sysMenu =new SysMenu();
    sysMenu.setId(id);
    sysMenu.setUpdateTime(new Date());
    sysMenu.setDelFlag(CommonConstant.STATUS_DEL);
        return new RT<>(sysMenuService.updateById(sysMenu));
        }

*/
/**
 * 编辑
 * @param  sysMenu 实体
 * @return success/false
 *//*

@PutMapping
public RT<Boolean> edit(@RequestBody SysMenu sysMenu){
    sysMenu.setUpdateTime(new Date());
        return new RT<>(sysMenuService.updateById(sysMenu));
        }
        */
        }

